﻿using SWBetongXaydung.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SWBetongXaydung.BLL
{
    class HanhTrinhBLL
    {
        public static List<HanhTrinh> GetList(int thang = 0, int nam = 0, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
                db = dbContext;
            else
                db = DBContextHelper.GetContext();
            if (thang == 0)
            {
                var list = (from e in db.HanhTrinhDbSet
                            orderby e.Ngay descending, e.SoPhieu descending
                            select e).ToList();
                if (dbContext == null)
                    db.Dispose();
                return list;
            }
            else
            {
                var list = (from e in db.HanhTrinhDbSet
                            where e.Ngay.Month == thang && e.Ngay.Year == nam
                            orderby e.Ngay descending, e.SoPhieu descending
                            select e).ToList();
                if (dbContext == null)
                    db.Dispose();
                return list;
            }
        }

        /// <summary>
        /// Lấy thông tin hành trình theo ID
        /// </summary>
        /// <param name="IDHanhTrinh"></param>
        /// <returns></returns>
        public static HanhTrinh Get(int IDHanhTrinh)
        {
            using (var db = DBContextHelper.GetContext())
            {
                return db.HanhTrinhDbSet.Where(e => e.IDHanhTrinh == IDHanhTrinh).SingleOrDefault();
            }
        }

        /// <summary>
        /// Lấy thông tin hành trình theo nơi đến
        /// </summary>
        /// <param name="IDHanhTrinh"></param>
        /// <returns></returns>
        public static HanhTrinh Get(string noiDen)
        {
            using (var db = DBContextHelper.GetContext())
            {
                return db.HanhTrinhDbSet.Where(e => e.NoiDen == noiDen).OrderByDescending(e => e.Ngay).FirstOrDefault();
            }
        }

        public static CODE_RESULT_RETURN Add(HanhTrinh ht, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
                db = dbContext;
            else
                db = DBContextHelper.GetContext();
            var obj = db.HanhTrinhDbSet.Where(e => e.Ngay == ht.Ngay && e.NoiDen == ht.NoiDen && e.IDTramCap == ht.IDTramCap).SingleOrDefault();
            if (obj != null)
                return CODE_RESULT_RETURN.MaTrung;
            else
            {
                db.HanhTrinhDbSet.Add(ht);
                if (ht.SoPhieu != 0)
                {
                    #region Rửa xe
                    var objRuaXe = db.HanhTrinhDbSet.Where(e => e.SoPhieu == -1 && e.Ngay == ht.Ngay).SingleOrDefault();
                    if (objRuaXe == null)
                    {
                        objRuaXe = new HanhTrinh
                        {
                            IDTramCap = ht.IDTramCap,
                            Ngay = ht.Ngay,
                            NoiDen = "Rửa xe",
                            DinhMucXeLon = 2,
                            DinhMucXeNho = 2,
                            SoPhieu = -1,
                            TinhTrang = 0
                        };
                        db.HanhTrinhDbSet.Add(objRuaXe);
                    }
                    #endregion

                    #region Điều xe đi Tứ hạ hoặc Huế
                    var objHueTuHa = db.HanhTrinhDbSet.Where(e => e.SoPhieu == -2 && e.Ngay == ht.Ngay).SingleOrDefault();
                    if (objHueTuHa == null)
                    {
                        objHueTuHa = new HanhTrinh
                            {
                                IDTramCap = ht.IDTramCap,
                                Ngay = ht.Ngay,
                                NoiDen = "Điều xe chuyển trạm",
                                SoPhieu = -2,
                                DinhMucXeNho = 20,
                                DinhMucXeLon = 25,
                                TinhTrang = 0
                            };
                        db.HanhTrinhDbSet.Add(objHueTuHa);
                    }
                    #endregion
                }
            }
            db.SaveChanges();
            if (dbContext == null)
                db.Dispose();
            return CODE_RESULT_RETURN.ThanhCong;
        }

        public static CODE_RESULT_RETURN Update(HanhTrinh ht, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
                db = dbContext;
            else
                db = DBContextHelper.GetContext();
            var obj = db.HanhTrinhDbSet.Where(e => e.IDHanhTrinh == ht.IDHanhTrinh).SingleOrDefault();
            if (obj != null)
            {
                obj.IDTramCap = ht.IDTramCap;
                obj.NoiDen = ht.NoiDen;
                obj.Ngay = ht.Ngay.Date;
                obj.SoPhieu = ht.SoPhieu;
                obj.DinhMucXeLon = ht.DinhMucXeLon;
                obj.DinhMucXeNho = ht.DinhMucXeNho;
                obj.TinhTrang = ht.TinhTrang;
                db.SaveChanges();
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThanhCong;
            }
            else
            {
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThatBai;
            }
        }

        public static CODE_RESULT_RETURN Delete(int IDHanhTrinh, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
                db = dbContext;
            else
                db = DBContextHelper.GetContext();
            var obj = db.HanhTrinhDbSet.Where(e => e.IDHanhTrinh == IDHanhTrinh).SingleOrDefault();
            if (obj != null)
            {
                var listDieuXe = obj.DieuXes.ToList();
                if (listDieuXe != null && listDieuXe.Count > 0)
                {
                    var sumChuyen = (from e in listDieuXe select e.SoChuyen).Sum();
                    if (sumChuyen != 0)
                        return CODE_RESULT_RETURN.ThatBai;
                    else
                    {
                        foreach (var item in listDieuXe)
                            db.DieuXeDbSet.Remove(item);
                    }
                }
                db.HanhTrinhDbSet.Remove(obj);
                db.SaveChanges();
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThanhCong;
            }
            else
            {
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThatBai;
            }
        }

        public static List<string> GetListNoiDen(BETONGXAYDUNGEntities dbContext = null)
        {
            if (dbContext != null)
                return dbContext.HanhTrinhDbSet.Where(e => e.SoPhieu > 0).Select(e => e.NoiDen).Distinct().ToList();
            using (var db = DBContextHelper.GetContext())
            {
                return db.HanhTrinhDbSet.Where(e => e.SoPhieu > 0).Select(e => e.NoiDen).Distinct().ToList();
            }
        }
    }
}
